package com.health_predict_springboot.controller;

import com.health_predict_springboot.pojo.SysUser;
import com.health_predict_springboot.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理", description = "用户相关的API接口")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Operation(summary = "根据ID获取用户信息")
    @GetMapping("/{userId}")
    public ResponseEntity<SysUser> getUserById(@PathVariable Long userId) {
        SysUser user = sysUserService.getUserById(userId);
        if (user != null) {
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @Operation(summary = "根据手机号获取用户信息")
    @GetMapping("/phone/{phone}")
    public ResponseEntity<SysUser> getUserByPhone(@PathVariable String phone) {
        SysUser user = sysUserService.getUserByPhone(phone);
        if (user != null) {
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @Operation(summary = "获取所有用户列表")
    @GetMapping("/list")
    public ResponseEntity<List<SysUser>> getAllUsers() {
        List<SysUser> users = sysUserService.getAllUsers();
        return ResponseEntity.ok(users);
    }

    @Operation(summary = "分页查询用户")
    @GetMapping("/page")
    public ResponseEntity<Map<String, Object>> getUsersByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer userType,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String keyword) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("offset", (pageNum - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("userType", userType);
        params.put("status", status);
        params.put("keyword", keyword);
        
        List<SysUser> users = sysUserService.getUsersByPage(params);
        int total = sysUserService.getUserCount(params);
        
        Map<String, Object> result = new HashMap<>();
        result.put("list", users);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "新增用户")
    @PostMapping
    public ResponseEntity<Integer> addUser(@RequestBody SysUser user) {
        int result = sysUserService.addUser(user);
        if (result > 0) {
            return ResponseEntity.status(HttpStatus.CREATED).body(result);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    @Operation(summary = "更新用户信息")
    @PutMapping
    public ResponseEntity<Integer> updateUser(@RequestBody SysUser user) {
        int result = sysUserService.updateUser(user);
        if (result > 0) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/{userId}")
    public ResponseEntity<Integer> deleteUser(@PathVariable Long userId) {
        int result = sysUserService.deleteUser(userId);
        if (result > 0) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public ResponseEntity<SysUser> login(@RequestBody Map<String, String> loginInfo) {
        String phone = loginInfo.get("phone");
        String password = loginInfo.get("password");
        
        SysUser user = sysUserService.login(phone, password);
        if (user != null) {
            // 登录成功，不返回密码
            user.setPassword(null);
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
    }

    @Operation(summary = "修改密码")
    @PostMapping("/changePassword")
    public ResponseEntity<Integer> changePassword(@RequestBody Map<String, Object> passwordInfo) {
        Long userId = Long.valueOf(passwordInfo.get("userId").toString());
        String oldPassword = passwordInfo.get("oldPassword").toString();
        String newPassword = passwordInfo.get("newPassword").toString();
        
        int result = sysUserService.changePassword(userId, oldPassword, newPassword);
        if (result > 0) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
        }
    }
}